package com.bg.utisl;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * @Auther: zhengshunzhi
 * @Date: 2018/12/24 17:28
 * @Description: 配置文件读取
 */
public class PropertieConfigurationUtil {
    private static Properties properties = null;

    private static List<String> sourcesPath = Arrays.asList("zk.properties", "jdbc.properties");


    static {
        properties = new Properties();
        // 使用InPutStream流读取properties文件
        InputStream in = null;

        try {
            for (String path : sourcesPath) {
                in = PropertieConfigurationUtil.class.getClassLoader().getResourceAsStream(path);
                properties.load(in);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 功能描述:根据key获取value
     *
     * @param:
     * @return:
     * @auther: bonree
     * @date: 2018/12/24 18:18
     */
    public static String getPropertie(String key) {
        return properties.getProperty(key);
    }

    /**
     * 功能描述: 转换为int类型的value
     *
     * @param:
     * @return:
     * @auther: bonree
     * @date: 2018/12/24 18:39
     */
    public static int getInteger(String key) {
        try {
            return Integer.valueOf(getPropertie(key));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 功能描述: 转换为boolean类型的value
     *
     * @param:
     * @return:
     * @auther: bonree
     * @date: 2018/12/24 18:39
     */
    public static boolean getBoolean(String key) {
        try {
            return Boolean.valueOf(getPropertie(key));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return false;
    }
}
